package duppentity.dto.merchant;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import constants.BuDefault;
import utils.JSONObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 将dupp返回的数据重新进行封装
 * Author: pengpeng@djcars.cn
 * Date: 2019/12/10 10:00
 */

public class SysAdminDtoUtil {
    public static List<SysAdminDto> getSysAdminDtoList(JsonObject data, boolean isEdit) {
        List<SysAdminDto> sysAdminDtoList = null;
        if (data != null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    if (list != null && list.size() > 0) {
                        sysAdminDtoList = new ArrayList<>();
                        for (Integer i = 0; i < list.size(); i++) {
                            JSONObject listOne = JSONObjectUtils.get(list, i);
                            SysAdminDto sysAdminDto = convertSysAdminDto(listOne, data, isEdit);
                            sysAdminDtoList.add(sysAdminDto);
                        }
                    }
                }
            }
        }
        return sysAdminDtoList;
    }

    public static SysAdminDto getSysAdminDto(JsonObject data, boolean isEdit) {
        SysAdminDto sysAdminDto =new SysAdminDto();
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    JSONObject listOne = JSONObjectUtils.get(list, 0);
                    sysAdminDto = convertSysAdminDto(listOne,data, isEdit);
                }
            }
        }
        return sysAdminDto;
    }

    private static SysAdminDto convertSysAdminDto(JSONObject listOne, JsonObject data, boolean isEdit) {
        SysAdminDto sysAdminDto = new SysAdminDto();

        sysAdminDto.id = listOne.getLongValue("id");
        sysAdminDto.invalid = listOne.getIntValue("invalid");
        sysAdminDto.type = listOne.getIntValue("type");
        sysAdminDto.typeName = BuDefault.getResultName(sysAdminDto.type,BuDefault.nameType.accountType);
        sysAdminDto.name = listOne.getString("name");
        sysAdminDto.mobile = listOne.getString("mobile");
        sysAdminDto.parentId = listOne.getLongValue("parentId");
        sysAdminDto.account = listOne.getString("account");
        sysAdminDto.createBy = listOne.getLongValue("createBy");
        sysAdminDto.createTime = listOne.getLongValue("createTime");
        JSONArray buMerchantCouponIds = listOne.getJSONArray("buMerchantCouponIds");
        if (buMerchantCouponIds != null && buMerchantCouponIds.size() > 0) {
            StringBuilder buCouponName = new StringBuilder();
            StringBuilder buCouponId = new StringBuilder();
            for (Object merchantCouponId : buMerchantCouponIds) {
                JSONObject buMerchantCoupon = JSONObjectUtils.get(data, "bu_merchant_coupon");
                JSONObject buCoupon = JSONObjectUtils.get(data, "bu_coupon");
                String couponId = JSONObjectUtils.getString(buMerchantCoupon, merchantCouponId.toString(), "couponId");
                String name = JSONObjectUtils.getString(buCoupon, couponId, "name");
                buCouponId.append(couponId).append(",");
                buCouponName.append(name).append(",");
            }
            sysAdminDto.couponId = buCouponId.substring(0, buCouponId.length() - 1);
            sysAdminDto.couponName = buCouponName.substring(0, buCouponName.length() - 1);
        }
        if (sysAdminDto.createBy > 0) {
            JSONObject sysAdmin = JSONObjectUtils.get(data, "sys_admin");
            sysAdminDto.createByName = JSONObjectUtils.getString(sysAdmin, sysAdminDto.createBy.toString(), "name");
        }
        JSONArray sysAdminStationIds = listOne.getJSONArray("sysAdminStationIds");
        if (sysAdminStationIds != null) {
            sysAdminDto.stations = sysAdminStationIds.size();
        }
        if (sysAdminStationIds != null && sysAdminStationIds.size() > 0) {
            for (Object sysAdminStationId : sysAdminStationIds) {
                JSONObject buStationFaultUser = JSONObjectUtils.get(data, "sys_admin_station");
                String stationId = JSONObjectUtils.getString(buStationFaultUser, sysAdminStationId.toString(), "stationId");
                JSONObject buStation = JSONObjectUtils.get(data, "bu_station");
                String name = JSONObjectUtils.getString(buStation, stationId, "name");
                sysAdminDto.stationNames.add(name);
                if (isEdit) {
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("adminStationId", sysAdminStationId.toString());
                    jsonObject.addProperty("name", name);
                    sysAdminDto.stationName.add(jsonObject);
                }
            }
        }
        JSONArray sysAdminRoleIds = listOne.getJSONArray("sysAdminRoleIds");
        if (sysAdminRoleIds != null && sysAdminRoleIds.size() > 0) {
            JSONObject sysRole = JSONObjectUtils.get(data, "sys_role");
            JSONObject sysAdminRole = JSONObjectUtils.get(data, "sys_admin_role");
            sysAdminDto.roleId = JSONObjectUtils.getLong(sysAdminRole, sysAdminRoleIds.get(0).toString(), "roleId");
            sysAdminDto.roleName = JSONObjectUtils.getString(sysRole, sysAdminDto.roleId.toString(), "name");
        }
        return sysAdminDto;
    }
}

